//
// Ishtar C++ Utility Library
//
// Task Facility - Periodic Timer Private Header
//

#ifndef __ISHTAR_TASK_PERIODIC_TIMER_IMPL_H
#define __ISHTAR_TASK_PERIODIC_TIMER_IMPL_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <Ishtar/Task/PeriodicTimer.h>
#include <Ishtar/Task/WorkImpl.h>
#include <Ishtar/Thread/Mutex.h>


namespace Ishtar
{

namespace Task
{

class ThreadPoolImpl;

///////////////////////////////////////////////////////////////////////////////
//
// Periodic Timer
// - currently only Thread Pool supports Timer.
//

class PeriodicTimerImpl : public ReferenceCounted
{
public:

    PeriodicTimerImpl(
        const std::string& name, const WorkFunction& workFunc, Uint dueTime, Uint period,
        WorkExecutor& executor
    );
    
    ~PeriodicTimerImpl();
    
    Bool Stop();
        
    
private:

    //
    // Wrapper Functions
    //
    
    void WorkWrapper( const WorkFunction& workFunc );
    
    void SubmitWorkWrapper( const WorkFunction& workFunc, Uint delay );


    //
    // Data Members
    //

    std::string m_name;
    Uint        m_period;
    Bool        m_stopped;
    
    WorkExecutor& m_executor;

    Work m_work;

    mutable Mutex m_mutex;
};

typedef boost::intrusive_ptr< PeriodicTimerImpl > PeriodicTimerImplPtr;


///////////////////////////////////////////////////////////////////////////////

} // namespace Task

} // namespace Ishtar

#endif // __ISHTAR_TASK_PERIODIC_TIMER_IMPL_H
